home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / INTERVIE / GRAPHIC / REF.H < prev    next >
C/C++ Source or Header  |  1992-01-10  |  5KB  |  144 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * Interface to Ref (persistent object reference) base class.
  25.  */
  26.  
  27. #ifndef ref_h
  28. #define ref_h
  29.  
  30. #include <stdio.h>
  31. #include <InterViews/Graphic/pfile.h>
  32. #include <InterViews/Graphic/objman.h>
  33. #include <InterViews/Graphic/persistent.h>
  34.  
  35. static const UID CLUSTERBITMASK = 0x80000000;
  36. static const UID INMEMORYBITMASK = 0x1;
  37.  
  38. class Ref {
  39.     friend class Cache;
  40.     friend class ObjectMan;
  41. public:
  42.     Ref();
  43.     Ref(UID);
  44.     Ref(Persistent*);
  45.     
  46.     UID uid();
  47.     Persistent* operator()();
  48.     boolean Valid();            /* true if ref is non-nil/non-INVALIDUID */
  49.  
  50.     boolean operator==(Ref r);
  51.     boolean operator!=(Ref r);
  52.  
  53.     boolean Write(PFile*);        /* write uid + cluster bit */
  54.     boolean Read(PFile*);        /* read uid + cluster bit */
  55.     boolean WriteObjects(PFile*);   /* write object if not head of a cluster */
  56.     boolean ReadObjects(PFile*);    /* read object if not head of a cluster */
  57. protected:
  58.     void uid (UID);                 /* set uid */
  59.  
  60.     void Warning(const char*);
  61.     void Panic(const char*, int);
  62.     
  63.     boolean inMemory();
  64.     boolean isCluster();            /* checks cluster bit (msb UID); it's */
  65.                     /* set if object is a head of a cluster */
  66.                     /* (doesn't check if ref is inMemory!) */
  67.     void setClusterBit();
  68.     void resetClusterBit();
  69.     UID getUID();                /* removes cluster bit */
  70.  
  71.     Persistent* ref();        /* returns object (possibly faulting it in) */
  72.     Persistent* refObjects();        /* seekless ref() for faulting in  */
  73.                     /* consecutive objects */
  74.     void unref();            /* converts ref to uid w/cluster bit */
  75. protected:
  76.     union {            /* distinguished in that a UID has lsb == 1 */
  77.     Persistent* refto;
  78.     UID _uid;
  79.     } info;
  80. };
  81.  
  82. /*
  83.  * inlines
  84.  */
  85.  
  86. inline Ref::Ref (Persistent* obj) {
  87.     info.refto = (obj == nil) ? (Persistent*) INVALIDUID : obj;
  88. }
  89.  
  90. inline UID Ref::uid () { return info._uid; }
  91. inline void Ref::uid (UID u) { info._uid = u; }
  92.  
  93. inline boolean Ref::inMemory () {
  94.     return (uid() & INMEMORYBITMASK) == 0;    /* test inMemory bit */
  95. }
  96.  
  97. inline boolean Ref::isCluster () { return (uid() & CLUSTERBITMASK) != 0; }
  98. inline void Ref::setClusterBit () { info._uid |= CLUSTERBITMASK; }
  99. inline void Ref::resetClusterBit () { info._uid &= ~CLUSTERBITMASK; }
  100.  
  101. inline Persistent* Ref::ref () {
  102.     if (uid() == INVALIDUID) {
  103.     return (Persistent*) nil;
  104.     } else if (info.refto != nil && !inMemory() && !TheManager->Find( this ) ) {
  105.     Panic( "unable to find object ", uid() );
  106.     return (Persistent*) nil;    // so the compiler won't squawk
  107.     } else {
  108.     return info.refto;
  109.     }
  110. }
  111.  
  112. inline Persistent* Ref::operator() () { return ref(); }
  113. inline boolean Ref::Valid () { return uid() != INVALIDUID && info.refto != nil; }
  114.  
  115. inline boolean Ref::operator!= (Ref r) { return ! (*this == r); }
  116.  
  117. inline boolean Ref::Write (PFile* f) {
  118.     unref();
  119.     return f->Write((int)uid());
  120. }
  121.  
  122. inline boolean Ref::WriteObjects (PFile* f) {
  123.     if (isCluster()) {
  124.     return true;        /* if it's the head of a cluster, we're not */
  125.                 /* responsible for writing it out */
  126.     } else if ( inMemory() || TheManager->IsCached(this) ) {
  127.     return info.refto->writeObjects(f);
  128.     } else if (uid() != INVALIDUID) {
  129.     Warning("object within a cluster not in memory prior to write");
  130.     return false;
  131.     } else {
  132.     return true;
  133.     }
  134. }
  135.  
  136. inline boolean Ref::ReadObjects (PFile*) {
  137.     if (!isCluster()) {
  138.     (void*) refObjects();
  139.     }
  140.     return true;
  141. }
  142.  
  143. #endif
  144.